#-*- coding: utf-8 -*-

from saltstack.models import salt_api_log

import ast
import datetime

def salt_log(user_name, minions, jobs_id, salt_type, len_salt_node, salt_cmd_lr, api_return):
    try:
        salt_shell_logs = salt_api_log(user_name=user_name, minions=minions, jobs_id=jobs_id, stalt_type=salt_type, salt_len_node=len_salt_node, stalt_input=salt_cmd_lr, api_return=api_return,)
        salt_shell_logs.save()
    except Exception,ex:
        print Exception,ex

def salt_log_save(user_name, minions, jobs_id, salt_type, len_salt_node, salt_cmd_lr, api_return,cursor):
    salt_shell_logs ='insert into salt_api_log set user_name="'+user_name+'", minions="'+minions+'", jobs_id="'+jobs_id\
                     + '", stalt_type="'+salt_type+'", salt_len_node='+len_salt_node+', stalt_input="'+salt_cmd_lr+'", api_return="'+api_return+'"'
    cursor.execute(salt_shell_logs)

def salt_data_log(start,end):
    context = {}
    try:
        count = salt_api_log.objects.all().count()
        list_api_return = []
        log_list = salt_api_log.objects.all().order_by("-id")[start:end]
        for i in log_list:
            minions = ast.literal_eval(i.minions)
            minionsStr = ''
            for idx in minions:
                if minionsStr == '':
                    minionsStr = idx
                else:
                    minionsStr += idx
            list_api_return.append({"minions":minionsStr, "log_time":i.log_time, "username":i.user_name, "jobs_id":i.jobs_id, "id":i.id, "stalt_type":i.stalt_type, "stalt_input":i.stalt_input, "salt_len_node":i.salt_len_node})
        context["logs"] = list_api_return
        context['count'] = count
    except Exception,ex:
        print Exception,ex
    return context

def get_salt_log(id):
    context = {}
    try:
        logRes = salt_api_log.objects.get(id=id)
        minions = ast.literal_eval(logRes.minions)
        minionsStr = ''
        for idx in minions:
            if minionsStr == '':
                minionsStr = idx
            else:
                minionsStr += idx

        # logRes.api_return = logRes.api_return.replace("\\\n","<br />")
        apiReturnStr = ''
        api_return = ast.literal_eval(logRes.api_return)

        for idx in api_return:
            # apiReturn = api_return[idx]
            if apiReturnStr == '':
                apiReturnStr = idx
            else:
                # apiReturn = apiReturn.replace("\n","<br />")
                apiReturnStr += '<br />' + idx
            apiReturnStr += '<br/><br />' + strDeal(api_return[idx])

        logRes.log_time = datetime.datetime.strftime(logRes.log_time,"%Y-%m-%d %H:%M:%S")
        context['log'] = {"api_return":apiReturnStr, "minions":minionsStr, "log_time":logRes.log_time, "username":logRes.user_name, "jobs_id":logRes.jobs_id, "id":logRes.id, "stalt_type":logRes.stalt_type, "stalt_input":logRes.stalt_input, "salt_len_node":logRes.salt_len_node}
    except Exception,ex:
        print Exception,ex
    return context

def strDeal(str):
    tmpStr = ''
    for tmp in str.splitlines():
        tmp=tmp.rstrip()+'<br/>'
        tmpStr=tmpStr + tmp
    return tmpStr


